package com.andox.fn.inf.rpc.rest.parser;


import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * @ClassName: AbstractContractDefinitionParser
 * @Description: TODO
 * @author: Andox
 * @date: 2019年7月29日 上午11:58:26
 * 
 */
public abstract class AbstractContractDefinitionParser extends AbstractSingleBeanDefinitionParser implements TagNameSupport {
	private static final String INTERFACE_ELEMENT = "interface";
	private static final String INTERFACE_PROPERTY = "interfaceClass";
	
	private static final String UNIQUE_ID_ELEMENT = "unique-id";
	private static final String UNIQUE_ID_PROPERTY = "uniqueId";

	@Override
	protected boolean shouldGenerateIdAsFallback() {
		return true;
	}

	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
		String id = element.getAttribute(ID_ATTRIBUTE);
		builder.addPropertyValue(ID_ATTRIBUTE, id);
		
		String interfaceType = element.getAttribute(INTERFACE_ELEMENT);
		builder.addPropertyValue(INTERFACE_PROPERTY, interfaceType);
		builder.getBeanDefinition().getConstructorArgumentValues().addIndexedArgumentValue(0, interfaceType);

		String uniqueId = element.getAttribute(UNIQUE_ID_ELEMENT);
		builder.addPropertyValue(UNIQUE_ID_PROPERTY, uniqueId);

		String registry = element.getAttribute("registry");
		builder.addPropertyValue("registry", registry);
		
		doParseInternal(element, parserContext, builder);
	}

	protected abstract void doParseInternal(Element element, ParserContext parserContext,
			BeanDefinitionBuilder builder);
}
